package com.lzh.tiktok.user.controller;

import com.lzh.tiktok.user.service.MessageService;
import com.lzh.tiktok.user.vo.CommonResponseVo;
import com.lzh.tiktok.user.vo.MessageResponseVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lizhihang
 * @create 2023-01-29 13:32
 */
@RestController
@RequestMapping("/douyin/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    /**
     * 在好友列表点击任意用户，进入详细聊天页面
     * 在该页面下会定时轮询调用这个查询消息的接口，获取最新消息列表。
     *
     * @param to_user_id 对方用户id
     * @param token      用户鉴权token
     * @return
     */
    @GetMapping("/chat")
    public MessageResponseVo chat(@RequestParam("to_user_id") String to_user_id,
                                  @RequestParam("token") String token) {
        MessageResponseVo messageResponseVo = messageService.chatRecords(to_user_id, token);
        return messageResponseVo;
    }


    //todo 撤销消息，删除消息....
    //当前登录用户向自己的好友发送消息
    @PostMapping("/action")
    public CommonResponseVo sendMessage(@RequestParam("to_user_id") String to_user_id,
                                        @RequestParam("token") String token,
                                        @RequestParam("action_type") String action_type,
                                        @RequestParam("content") String content) {

        Boolean isSuccess = false;
        //action_type = 1代表是发送消息
        if ("1".equals(action_type)) {
            isSuccess = messageService.sendMessage(to_user_id, token, content);
        }
        return isSuccess ? CommonResponseVo.success() : CommonResponseVo.fail();
    }

}
